package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Slf4j
@Aspect
@Component
public class AutoFillAspect {
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut(){}

    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始对公共字段进行填充，填充的方法是{}",joinPoint.getSignature().getName());
        //获取方法实体对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        //获取方法上的注解
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);

        //获取到枚举值
        OperationType value = annotation.value();

        //获取参数数组
        Object[] args = joinPoint.getArgs();

        //判断参数是否空
        if (args == null || args.length == 0) {
            return;
        }

        //获取字段对象
        Object entry = args[0];

        //设置公共字段需要使用的值
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();

        //判断是什么类型
        if (value == OperationType.INSERT) {
            try {
                /*entry.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class).invoke(entry,now);
                entry.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class).invoke(entry,now);
                entry.getClass().getDeclaredMethod("setCreateUser", Long.class).invoke(entry,currentId);
                entry.getClass().getDeclaredMethod("setUpdateUser", Long.class).invoke(entry,currentId);*/
                Method setCreateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //通过反射为对象属性赋值
                setCreateTime.invoke(entry,now);
                setCreateUser.invoke(entry,currentId);
                setUpdateTime.invoke(entry,now);
                setUpdateUser.invoke(entry,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if (value == OperationType.UPDATE) {
            try {
                entry.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class).invoke(entry,now);
                entry.getClass().getDeclaredMethod("setUpdateUser", Long.class).invoke(entry,currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
